package com.mao.action.voucher;

import java.math.BigDecimal;
import java.util.Date;

import com.mao.action.BaseAction;
import com.mao.action.jsonvo.voucher.JsonVoVoucher;
import com.mao.model.organization.Organization;
import com.mao.model.product.Product;
import com.mao.model.voucher.ProductReturnWareHouseVoucher;
import com.mao.model.voucher.ReturnWareHouseVoucher;
import com.mao.model.voucher.VoucherType;
import com.mao.model.voucher.VoucherVerityType;
import com.mao.model.warehouse.WareHouse;
import com.mao.service.common.ICommonService;
import com.mao.service.organization.IOrganizationService;
import com.mao.service.product.IProductService;
import com.mao.service.productstock.IProductStockService;
import com.mao.service.searchvoucher.ISearchVoucherService;
import com.mao.service.voucher.IProductReturnWareHouseVoucherService;
import com.mao.service.voucher.IReturnWareHouseVoucherService;
import com.mao.service.warehouse.IWareHouseService;
import com.mao.util.SessionHelper;

public class ReturnWareHouseVoucherAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private ReturnWareHouseVoucher rwv;

	private IReturnWareHouseVoucherService returnWareHouseVoucherService;

	private IOrganizationService organizationService;

	private IWareHouseService wareHouseService;

	private IProductService productService;

	private IProductStockService productStockService;

	private IProductReturnWareHouseVoucherService prwvService;

	private ICommonService commonService;

	private Organization organization;

	private WareHouse wareHouse;

	private String products;

	private JsonVoVoucher returnVoucher;

	private ProductReturnWareHouseVoucher prwv;

	private ISearchVoucherService searchVoucherService;

	private String id;

	public String execute() {
		if (id != null) {
			rwv = (ReturnWareHouseVoucher) searchVoucherService.getVoucherById(
					VoucherType.returnwarehouse.getVoucherClassName(),
					Long.valueOf(id));
		}
		return SUCCESS;
	}

	public String saveReturnWareHouseVoucher() {
 		organization = organizationService.getOrganizationById(rwv.getOrg()
				.getId());
		wareHouse = wareHouseService.getWareHouseById(rwv.getWareHouse()
				.getId());
		rwv.setStore(SessionHelper.getSessionUser().getStore());
		rwv.setOrg(organization);
		rwv.setWareHouse(wareHouse);
		rwv.setDatetime(new Date()); 
		rwv.setVoucherType(VoucherType.returnwarehouse);
		rwv.setIsverity(VoucherVerityType.notverity);
		String voucherNo = commonService.getVoucherNo(SessionHelper
				.getSessionUser().getId(), "JHTD",
				ReturnWareHouseVoucher.class.getName());
		rwv.setVoucherNo(voucherNo);
		rwv.setUser(SessionHelper.getSessionUser());
		returnWareHouseVoucherService.saveReturnWareHouseVoucher(rwv);
		String[] prosArray = products.split(",");
		String[] pro = null;
		Product singlePro = null;
		Integer proAmount = 0;
		BigDecimal totalPrice = new BigDecimal(0); 
		for (int i = 0; i < prosArray.length; i++) {
			prwv = new ProductReturnWareHouseVoucher();
			pro = prosArray[i].split("_");
			singlePro = productService.getProductById(Long.valueOf(pro[0]));
			prwv.setProduct(singlePro);
			prwv.setUnitPrice(BigDecimal.valueOf(Double.valueOf(pro[1])));
			prwv.setAmount(Integer.valueOf(pro[2]));
			// 商品总量计算
			proAmount = proAmount + prwv.getAmount();
			prwv.setSubtotal(BigDecimal.valueOf(Double.valueOf(pro[3])));
			// 单据总价计算
			totalPrice = totalPrice.add(prwv.getSubtotal());
			prwv.setVoucher(rwv);
			prwv.setStore(SessionHelper.getSessionUser().getStore());
			prwv.setVoucherType(VoucherType.returnwarehouse);
			prwvService.savePrwv(prwv);
		}
		// 再存单据的商品总量
		rwv.setProAmount(proAmount);
		// 再存单据的总价
		rwv.setTotalPrice(totalPrice);
		returnWareHouseVoucherService.saveReturnWareHouseVoucher(rwv);
		returnVoucher = new JsonVoVoucher();
		returnVoucher.setId(rwv.getId());
		returnVoucher.setVoucherNo(voucherNo);
		return SUCCESS;
	}

	// 审核单据
	public String veriyVoucher() {
		organization = organizationService.getOrganizationById(rwv.getOrg()
				.getId());
		wareHouse = wareHouseService.getWareHouseById(rwv.getWareHouse()
				.getId());
		ReturnWareHouseVoucher updateRwv = (ReturnWareHouseVoucher) searchVoucherService
				.getVoucherById(
						VoucherType.returnwarehouse.getVoucherClassName(),
						Long.valueOf(id));
		updateRwv.setTransactionAmount(rwv.getTransactionAmount());
		updateRwv.setReceiveAmount(rwv.getReceiveAmount());
		updateRwv.setNo(rwv.getNo());
		updateRwv.setNote(rwv.getNote());
		updateRwv.setOrg(organization);
		updateRwv.setWareHouse(wareHouse);
		updateRwv.setUpdatetime(new Date());
		updateRwv.setIsverity(VoucherVerityType.verityed);
		updateRwv.setUser(SessionHelper.getSessionUser());
		returnWareHouseVoucherService.saveReturnWareHouseVoucher(updateRwv);
		String[] prosArray = products.split(",");
		String[] pro = null;
		Product singlePro = null;
		Integer proAmount = 0;
		BigDecimal totalPrice = new BigDecimal(0); 
		for (int i = 0; i < prosArray.length; i++) {
			prwv = new ProductReturnWareHouseVoucher();
			pro = prosArray[i].split("_");
			singlePro = productService.getProductById(Long.valueOf(pro[0]));
			prwv.setProduct(singlePro);
			prwv.setUnitPrice(BigDecimal.valueOf(Double.valueOf(pro[1])));
			prwv.setAmount(Integer.valueOf(pro[2]));
			proAmount = proAmount + prwv.getAmount();
			// 再存单据的商品总量
			prwv.setSubtotal(BigDecimal.valueOf(Double.valueOf(pro[3])));
			// 单据总价计算
			totalPrice = totalPrice.add(prwv.getSubtotal());
			prwv.setVoucher(updateRwv);
			prwv.setStore(SessionHelper.getSessionUser().getStore());
			prwv.setVoucherType(VoucherType.returnwarehouse);
			prwvService.savePrwv(prwv);
		}
		// 再存单据的商品总量
		updateRwv.setProAmount(proAmount);
		// 再存单据的总价
		updateRwv.setTotalPrice(totalPrice);
		returnWareHouseVoucherService.saveReturnWareHouseVoucher(updateRwv);
		returnVoucher = new JsonVoVoucher();
		returnVoucher.setId(updateRwv.getId());
		returnVoucher.setVoucherNo(updateRwv.getVoucherNo());
		return SUCCESS;
	}

	public String toAddWareHouseVoucher() {
		return SUCCESS;
	}

	public IOrganizationService getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(IOrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public IWareHouseService getWareHouseService() {
		return wareHouseService;
	}

	public void setWareHouseService(IWareHouseService wareHouseService) {
		this.wareHouseService = wareHouseService;
	}

	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	public WareHouse getWareHouse() {
		return wareHouse;
	}

	public void setWareHouse(WareHouse wareHouse) {
		this.wareHouse = wareHouse;
	}

	public String getProducts() {
		return products;
	}

	public void setProducts(String products) {
		this.products = products;
	}

	public IProductService getProductService() {
		return productService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public ReturnWareHouseVoucher getRwv() {
		return rwv;
	}

	public void setRwv(ReturnWareHouseVoucher rwv) {
		this.rwv = rwv;
	}

	public IReturnWareHouseVoucherService getReturnWareHouseVoucherService() {
		return returnWareHouseVoucherService;
	}

	public void setReturnWareHouseVoucherService(
			IReturnWareHouseVoucherService returnWareHouseVoucherService) {
		this.returnWareHouseVoucherService = returnWareHouseVoucherService;
	}

	public IProductReturnWareHouseVoucherService getPrwvService() {
		return prwvService;
	}

	public void setPrwvService(IProductReturnWareHouseVoucherService prwvService) {
		this.prwvService = prwvService;
	}

	public ProductReturnWareHouseVoucher getPrwv() {
		return prwv;
	}

	public void setPrwv(ProductReturnWareHouseVoucher prwv) {
		this.prwv = prwv;
	}

	public ICommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	public JsonVoVoucher getReturnVoucher() {
		return returnVoucher;
	}

	public void setReturnVoucher(JsonVoVoucher returnVoucher) {
		this.returnVoucher = returnVoucher;
	}

	public ISearchVoucherService getSearchVoucherService() {
		return searchVoucherService;
	}

	public void setSearchVoucherService(
			ISearchVoucherService searchVoucherService) {
		this.searchVoucherService = searchVoucherService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public IProductStockService getProductStockService() {
		return productStockService;
	}

	public void setProductStockService(IProductStockService productStockService) {
		this.productStockService = productStockService;
	}

}
